package javacode.org.ssh.common.base.dao.hibernate;

import java.io.Serializable;
import java.util.List;

import javacode.org.ssh.common.base.dao.BaseDao;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;


@Component (value="baseDao")
public class BaseDaoHibernate  <T, PK extends Serializable> extends HibernateDaoSupport  implements BaseDao<T, PK> {

	private SessionFactory mySessionFactory ;
	protected Log log = LogFactory.getLog(this.getClass());
	private Class<T> persistentClass ;
	
	@SuppressWarnings("unchecked")
	public BaseDaoHibernate() {
		T t = (T)new Object();
		this.persistentClass = (Class<T>) t.getClass();
    }
	
	@Resource
	public void initSuperClassSessionFactory(@Qualifier("sessionFactory") SessionFactory sessionFactory){
		this.mySessionFactory = sessionFactory;
	}
	@PostConstruct
	public void injectSuperClassSessionFactory(){
		super.setSessionFactory(this.mySessionFactory);
	}
    /**
     * Constructor that takes in a class to see which type of entity to persist
     * @param persistentClass the class type you'd like to persist
     */
    public BaseDaoHibernate(final Class<T> persistentClass) {
        this.persistentClass = persistentClass;
    }

	public T get(PK id) {
		T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);
        return entity;
	}
	public Object get(Class<T> clazz, Serializable id) {
		   Object o = getHibernateTemplate().get(clazz, id);
	        return o;
	}
	public List<T> getAll() {
		 return super.getHibernateTemplate().loadAll(this.persistentClass);
	}

	public List<T> getAll(Class<T> clazz) {
		return getHibernateTemplate().loadAll(clazz);
	}

	public T save(T object) {
		return (T) super.getHibernateTemplate().merge(object);
	}
	
	public Object saveObject(Object obj){
		return super.getHibernateTemplate().merge(obj);
	}
	public void remove(PK id) {
		 super.getHibernateTemplate().delete(this.get(id));
	}

	public void remove(Class<T> clazz, Serializable id) {
		getHibernateTemplate().delete(get(clazz, id));		
	}

	public boolean exists(PK id) {
		T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);
       return entity != null;
	}
	public boolean exists(Class<T> clazz, Serializable id) {
		T entity = (T) super.getHibernateTemplate().get(clazz, id);
       return entity != null;
	}
	
	public boolean contains(Object obj){
		return super.getHibernateTemplate().contains(obj);
	}

	@Override
	public List<T> getList(int startIndex, int length) {
		Session session = this.getSession();
		Transaction transaction = session.beginTransaction();
		String hql = "from "+persistentClass.getClass();
//		transaction.begin();
		return null;
	}

	@Override
	public List<T> getList(int startIndex, int length, String paramter) {
		// TODO Auto-generated method stub
		return null;
	}


}
